<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;


/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class HuibaoController extends WeixinController {

    /**
     * 业绩汇报
     */
    public function fb(){
    	$openid = array('oaPkKxJ6ghXGf-Q7PgRf4UvC7UOc','oaPkKxNMUC36FCTMRpvqpX0Kp-ZM','oaPkKxIYuh7PGpQMH0RMvLI_v2gA','oaPkKxHbqzwzNCGJh5N_byGp0ztk','oaPkKxNEfpKn4ByVXFcwbEt-2BgE');
		//$openid = array('oaPkKxNEfpKn4ByVXFcwbEt-2BgE');
    	$data   = $this->rw();
    	$huibao = '建单量：'.$data['jd'].'\n'
				  .'获取微信：'.$data['qq'].'\n'
				  .'获取手机：'.$data['phone'].'\n'
				  .'邀约进店量：'.$data['yytime'].'\n'
				  .'邀约率：'.$data['yyl'].'%\n'
				  .'进店量：'.$data['jindtime'].'\n'
				  .'进店率：'.$data['jindl'].'%\n'
				  .'未到店量：'.$data['wddtime'].'\n'
				  .'未到店率：'.$data['wddl'].'%\n'
				  .'线上流失：'.$data['xslstime'].'\n'
				  .'线下流失：'.$data['xxlstime'].'\n'
				  .'总流失量：'.$data['lslz'].'\n'
				  .'流失率：'.$data['lslvz'].'%\n'
				  .'线上成交：'.$data['xscj'].'\n'
				  .'线下成交：'.$data['xxcj'].'\n'
				  .'总成交量：'.$data['zcj'].'\n'
				  .'成交率：'.$data['zcjl'].'%\n'
				  .'总成交金额：'.$data['setmoney'].'\n'
				  .'平均成交金额：'.$data['moneyp'];
		foreach ($openid as $key => $value) {
			$this->hb($value,$huibao);
		} 
    }
    public function rw(){
    	$zuoritime = date("Y-m-d",strtotime("-1 day"));
    	$begin     = strtotime($zuoritime);  //当前时间
        $end       = $begin+86399; 
        $index = 1; 
        while ($index<2) 
        { 
            $zj['jd']       =  $this->map1($begin,$end,$mm='jd');
            $zj['qq']       =  $this->map1($begin,$end,$mm='qq');
            $zj['phone']    =  $this->map1($begin,$end,$mm='phone');
            $zj['yytime']   =  $this->map1($begin,$end,$mm='yytime');
            $zj['yyl']      =  number_format($zj['yytime']/$zj['jd'],2)*100;
			$zj['wddtime']  =  $this->map1($begin,$end,$mm='wddtime');  //未到店量
			$zj['wddl']     =  number_format($zj['wddtime']/$zj['yytime'],2)*100; //未到店率
			$zj['jindtime'] =  $this->map1($begin,$end,$mm='jindtime');  //未到店量
			$zj['jindl']    =  number_format($zj['jindtime']/$zj['yytime'],2)*100; //未到店率
            $zj['xslstime'] =  $this->map1($begin,$end,$mm='xslstime'); //上线流失量
			$zj['xxlstime'] =  $this->map1($begin,$end,$mm='xxlstime'); //下线流失量
			$zj['lslz']     =  $zj['xslstime'] + $zj['xxlstime']; //流失量总计;
            $zj['lslvz']    =  number_format($zj['lslz']/$zj['yytime'],2)*100; //总流失率
			$zj['xslszb']   =  number_format($zj['xslstime']/$zj['lslz'],2)*100; //线上流失率
			$zj['xxlszb']   =  number_format($zj['xxlstime']/$zj['lslz'],2)*100; //线下流失率
			$zj['xxsdtime'] =  $this->map1($begin,$end,$mm='xxsdtime'); //线下死单量
            $zj['xscj']     =  $this->map1($begin,$end,$mm='xscj'); //上线成交量
            $zj['xxcj']     =  $this->map1($begin,$end,$mm='xxcj')+$this->map1($begin,$end,$mm='lshmcj'); //下线成交量
            $zj['zcj']      =  $zj['xscj']+$zj['xxcj']; //总成交率
            $zj['zcjl']     =  number_format($zj['zcj']/$zj['yytime'],2)*100; //总成交率
            $zj['xscjzb']   =  number_format($zj['xscj']/$zj['zcj'],2)*100; //线上成交占比
            $zj['xxcjzb']   =  number_format($zj['xxcj']/$zj['zcj'],2)*100; //线下成交占比
            $zj['setmoney'] =  $this->money($begin,$end);
            $zj['moneyp']   =  number_format($zj['setmoney']/$zj['zcj'],0);
            $index++; 

        } 
        return $zj;

    }

	public function hb($openid,$huibao){
		$template_id = 'dOSgBcW93hkD49xo99LBsRyxuYECm73O6lOpJrLG4fk'; //模板ID
		//发送微信接口
		//$openid = 'oaPkKxNEfpKn4ByVXFcwbEt-2BgE';
		$fbtime = date('Y-m-d H:i',time());
		$res    = $this->hbtz($openid,$template_id,$fbtime,$huibao);
		var_dump($res);
	}

	//业务查询
	public function map1($begin,$end,$mm,$qudao=false){
            $map['puth']         =  array('eq','1');
            if($qudao){
                $map['qd']       =  array('eq',$qudao);
            }
            if($mm == 'jd'){
				$map['ywstate']   = array('not in','66');
                $map['jdtime']   = array('between',array($begin,$end));
            }
            if($mm == 'qq'){
				$map['ywstate']   = array('not in','66');
                $map['qq']       = array('gt','0');
                $map['jdtime']   = array('between',array($begin,$end));
            }
            if($mm == 'phone'){
				$map['ywstate']   = array('not in','66');
                $map['phone']       = array('gt','0');
                $map['jdtime']   = array('between',array($begin,$end));
            }
			if($mm == 'wddtime'){
				$map['ywstate']   = array('not in','66');
                $map['wddtime']   = array('between',array($begin,$end));    
            }
			if($mm == 'jindtime'){
				$map['ywstate']   = array('not in','66');
                $map['jindtime']   = array('between',array($begin,$end));    
            }
            if($mm == 'yytime'){
				$map['ywstate']   = array('not in','66');
                $map['yytime']   = array('between',array($begin,$end));    
            }
            if($mm == 'xslstime'){
                $map['ywstate']       = array('eq','65');
                $map['lstime']   = array('between',array($begin,$end));
            }
			if($mm == 'xxlstime'){
                $map['ywstate']       = array('eq','149');
                $map['lstime']   = array('between',array($begin,$end));
            }
            if($mm == 'xscj'){
                $map['ywstate']       = array('eq','67');
                $map['ddtime']   = array('between',array($begin,$end));
            }
            if($mm == 'xxcj'){
                $map['ywstate']       = array('in','115,151');
                $map['ddtime']   = array('between',array($begin,$end));
            }
			if($mm == 'lshmcj'){
                $map['ywstate']       = array('eq','151');
                $map['ddtime']   = array('between',array($begin,$end));
            }
            $data   = D('Yewu')->where($map)->count();
            return $data;

    }
    //金钱
    public function money($sj_date,$end){
        if($end){
            $map['ddtime']   = array('between',array($sj_date,$end));
        }else{
            $map['ddtime']   = array('between',array($sj_date,$sj_date+86399));
        }
       
        $data   =  D('Yewu')->where($map)->sum('setmoney');       
        return $data;
    }

}
